package cz.data.domain.visual.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.visual.mapstruct.BoardMapper;
import cz.data.domain.visual.model.dto.BoardDto;
import cz.data.domain.visual.model.entity.BoardEntity;
import cz.data.domain.visual.model.query.BoardQuery;
import cz.data.domain.visual.model.vo.BoardVo;
import cz.data.domain.visual.service.BoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"可视化看板配置"})
@RestController
@RequestMapping("/boards")
public class BoardController extends BaseController {

    @Autowired
    private BoardService boardService;

    @Autowired
    private BoardMapper boardMapper;

    @ApiOperation(value = "可视化看板配置-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<BoardVo> getBoardById(@PathVariable String id) {
        BoardEntity boardEntity = boardService.getBoardById(id);
        return new SimpleDataResponse<>(boardMapper.toVO(boardEntity));
    }

    @ApiOperation(value = "可视化看板配置-分页查询")
    @ApiImplicitParam(name = "boardQuery", value = "查询实体boardQuery", required = true, dataTypeClass = BoardQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<BoardVo>> getBoardPage(BoardQuery boardQuery) {
        QueryWrapper<BoardEntity> queryWrapper = new QueryWrapper<>();
        IPage<BoardEntity> page = boardService.page(new Page<>(boardQuery.getPageNum(), boardQuery.getPageSize()), queryWrapper);
        List<BoardVo> collect = page.getRecords().stream().map(boardMapper::toVO).collect(Collectors.toList());
        JsonPage<BoardVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "可视化看板配置-新增")
    @ApiImplicitParam(name = "board", value = "详细实体board", required = true, dataType = "BoardDto")
    @PostMapping()
    public SimpleDataResponse<BoardVo> saveBoard(@RequestBody @Validated({ValidationGroups.Insert.class}) BoardDto board) {
        BoardEntity boardEntity = boardService.saveBoard(board);
        return new SimpleDataResponse<>(boardMapper.toVO(boardEntity));
    }

    @ApiOperation(value = "可视化看板配置-修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "board", value = "详细实体board", required = true, dataType = "BoardDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<BoardVo> updateBoard(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) BoardDto board) {
        BoardEntity boardEntity = boardService.updateBoard(board);
        return new SimpleDataResponse<>(boardMapper.toVO(boardEntity));
    }

    @ApiOperation(value = "可视化看板配置-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteBoardById(@PathVariable String id) {
        boardService.deleteBoardById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "可视化看板配置-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteBoardBatch(@PathVariable List<String> ids) {
        boardService.deleteBoardBatch(ids);
        return BaseResponse.success();
    }

    @PostMapping("/copy/{id}")
    public BaseResponse copyBoard(@PathVariable String id) {
        boardService.copyBoard(id);
        return BaseResponse.success();
    }

    @PutMapping("/build/{id}")
    public BaseResponse buildBoard(@PathVariable String id, @RequestBody BoardDto board) {
        boardService.buildBoard(board);
        return BaseResponse.success();
    }
}
